package org.jsola.hr.web;

import org.apache.commons.io.FilenameUtils;
import org.jsola.admin.vo.HeaderFieldsVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.HrCustomerDetailsNewAddDTO;
import org.jsola.hr.query.HrEmployeeSalaryNewQuery;
import org.jsola.hr.query.HrEmployeeSalaryQuery;
import org.jsola.hr.service.IHrCustomerDetailsNewService;
import org.jsola.hr.vo.HrMonthlySalarySummaryNewVO;
import org.jsola.hr.vo.JfStatisticsAnalyseVO;
import org.jsola.salary.entity.CustomerDetailsNewDO;
import org.jsola.salary.vo.CustomerDetailsNewVO;
import org.jsola.salary.vo.EmployeeDetailsNewVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 客户明细表
 *
 * @author JanusMix
 */
@RestController("hrHrCustomerDetailsNewController")
@RequestMapping(HrConstants.API_PATH + "/v1/customerDetailsNew")
public class HrCustomerDetailsNewController implements IHrCustomerDetailsNewControllerAPI {

    @Autowired
    private IHrCustomerDetailsNewService hrCustomerDetailsNewService;


    @Override
    @PostMapping("/saveByCompanyIds")
    public Result<List<CustomerDetailsNewVO>> saveByCompanyIds(@Valid @RequestBody HrCustomerDetailsNewAddDTO hrCustomerDetailsNewAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增客户明细表权限校验

        List<CustomerDetailsNewDO> customerDetailsNewDOList = hrCustomerDetailsNewService
                .saveByCompanyIds(hrCustomerDetailsNewAddDTO, tokenUser);
        ParamException.notEmpty(customerDetailsNewDOList, "新增客户明细失败，请刷新后重试");

        return Result.success(
                customerDetailsNewDOList.parallelStream().map(customerDetailsNewDO
                        -> customerDetailsNewDO.to(CustomerDetailsNewVO.class)).collect(Collectors.toList())
        );
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] customerDetailsNewIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除客户明细表权限校验

        int count = hrCustomerDetailsNewService.deleteByIds(tokenUser, customerDetailsNewIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping("/selectLastOne")
    public Result<CustomerDetailsNewDO> selectLastOne() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除客户明细表权限校验

        return Result.success(
                hrCustomerDetailsNewService.selectLastOne(tokenUser)
        );
    }

    @Override
    @GetMapping("/selectSalarySummaryBySiteAndMonthly")
    public Result<Page<HrMonthlySalarySummaryNewVO>> selectSalarySummaryBySiteAndMonthly(
            @Valid Integer yearly, Integer monthly, String customerDetailsType, String companyId, String companyName, Integer pageNo, Integer pageSize) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                hrCustomerDetailsNewService.selectSalarySummaryBySiteAndMonthly(yearly, monthly, customerDetailsType
                        , companyId, companyName, pageNo, pageSize, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectCustomGroupingBySiteAndMonthly")
    public Result<Page<HrMonthlySalarySummaryNewVO>> selectCustomGroupingBySiteAndMonthly(
            @Valid Integer yearly, Integer monthly, String customerDetailsType, Integer pageNo, Integer pageSize) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                hrCustomerDetailsNewService.selectCustomGroupingBySiteAndMonthly(yearly, monthly, customerDetailsType
                        , pageNo, pageSize, tokenUser)
        );
    }

    @Override
    @PostMapping("/selectHeaderDetailNew")
    public Result<HeaderFieldsVO> selectHeaderDetailNew(@Valid @RequestBody HrEmployeeSalaryQuery hrEmployeeSalaryQuery) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                hrCustomerDetailsNewService.selectHeaderDetailNew(hrEmployeeSalaryQuery.getType(), hrEmployeeSalaryQuery.getId()
                        , hrEmployeeSalaryQuery.getCustomerDetailsIdList(), hrEmployeeSalaryQuery.getYearly()
                        , hrEmployeeSalaryQuery.getMonthly(), tokenUser)
        );
    }

    @Override
    @PostMapping("/selectEmployeeDetailsPageByCompanyIdsNew")
    public Result<Page<EmployeeDetailsNewVO>> selectEmployeeDetailsPageByCompanyIdsNew(
            @Valid @RequestBody HrEmployeeSalaryNewQuery hrEmployeeSalaryNewQuery) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                hrCustomerDetailsNewService.selectEmployeeDetailsPageByCompanyIdsNew(hrEmployeeSalaryNewQuery, tokenUser)
        );
    }
    @Override
    @PostMapping("/exportNew")
    public ResponseEntity<Resource> exportNew(@Valid @RequestBody HrEmployeeSalaryNewQuery hrEmployeeSalaryNewQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        String excelFilePath = hrCustomerDetailsNewService.exportExcelNew(hrEmployeeSalaryNewQuery, tokenUser);
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @GetMapping("/jfStatisticsAnalyseLaborCosts")
    public Result<JfStatisticsAnalyseVO> jfStatisticsAnalyseLaborCosts(@Valid String companyId, Integer yearly) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                hrCustomerDetailsNewService.jfStatisticsAnalyseLaborCosts(companyId, yearly, tokenUser)
        );
    }

}
